package com.hp.controller;

import com.hp.pojo.Album;
import com.hp.service.AlbumService;
import com.hp.util.PageResult;
import com.hp.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/album")
public class AlbumController {

    @Resource
    private AlbumService albumService;

    @RequestMapping("/show")
    public String Album(){
        return "album";
    }

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult<Album> findPage(int pageNum, int pageSize, @RequestBody Album album){
        PageResult pageResult = albumService.findList(pageNum, pageSize,album);
        return pageResult;
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Album album){
        try {
            albumService.add(album);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody Album album){
        try {
            albumService.update(album);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @GetMapping("/findById")
    @ResponseBody
    public Album findById(int id){
        Album byId = albumService.findById(id);
        return byId;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Result delete(int id){
        try {
            albumService.delete(id);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }

    @RequestMapping("/showImages")
    public String showImages(int id,HttpSession session){
        System.out.println(id);
        session.setAttribute("Iid",id);
        return "ImageItems";
    }

    @RequestMapping("/getImageItems")
    @ResponseBody
    public String getImageItems(HttpSession session){
        Integer id = (Integer)session.getAttribute("Iid");
        System.out.println(id);
        return albumService.getimageItems(id);
    }
}
